package com.dinadi.yidi.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dinadi.yidi.R;
import com.dinadi.yidi.base.BaseFragment;
import com.dinadi.yidi.bean.eventbus.EventUserInfo;
import com.dinadi.yidi.http.interfaces.IntegralChangeInterface;
import com.dinadi.yidi.ui.MyApplication;
import com.dinadi.yidi.ui.activity.ClassificationActivity;
import com.dinadi.yidi.ui.activity.CreativeHotspotDetailsActivity;
import com.dinadi.yidi.ui.activity.FeedBackActivity;
import com.dinadi.yidi.ui.activity.IntegralCenterActivity;
import com.dinadi.yidi.ui.activity.LoginActivity;
import com.dinadi.yidi.ui.activity.MessageActivity;
import com.dinadi.yidi.ui.activity.MyActivitysActivity;
import com.dinadi.yidi.ui.activity.MyCollectionActivity;
import com.dinadi.yidi.ui.activity.MyCouponsActivity;
import com.dinadi.yidi.ui.activity.MyFollowsActivity;
import com.dinadi.yidi.ui.activity.MyOrderActivity;
import com.dinadi.yidi.ui.activity.MyShoppingCartsActivity;
import com.dinadi.yidi.ui.activity.ProductDetailsActivity;
import com.dinadi.yidi.ui.activity.SettledCooperationActivtiy;
import com.dinadi.yidi.ui.activity.UserSettingsActivity;
import com.dinadi.yidi.ui.view.ShareMenuPopupWindow;
import com.dinadi.yidi.utils.ImageUtils;
import com.dinadi.yidi.utils.SharePreferencesHelper;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import java.util.HashMap;

import de.greenrobot.event.EventBus;
import de.hdodenhof.circleimageview.CircleImageView;


public class PersonalFragment extends BaseFragment {
    private View view;
    private CircleImageView head_img;
    private TextView user_name, goto_login;
    private ImageView settings;
    private RelativeLayout user_proposal, user_msg, user_cooperation, user_share, user_collection, user_follows, my_activities, user_order;
    private RelativeLayout my_coupons, user_shopping_cart, my_integral_center;
    private ShareMenuPopupWindow selectImage;
    private boolean loginState;
    private int sharePosition;
    private String token, userId;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void noWifiRefresh() {

    }

    @Override
    protected View getLayout() {
        view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_personal, null);
        return view;
    }

    @Override
    protected void initView() {
        changePager(2);
        head_img = (CircleImageView) view.findViewById(R.id.head_img);
        user_name = (TextView) view.findViewById(R.id.user_name);
        user_proposal = (RelativeLayout) view.findViewById(R.id.user_proposal);
        user_msg = (RelativeLayout) view.findViewById(R.id.user_msg);
        user_cooperation = (RelativeLayout) view.findViewById(R.id.user_cooperation);
        settings = (ImageView) view.findViewById(R.id.settings);
        user_share = (RelativeLayout) view.findViewById(R.id.user_share);
        user_collection = (RelativeLayout) view.findViewById(R.id.user_collection);
        user_follows = (RelativeLayout) view.findViewById(R.id.user_follows);
        goto_login = (TextView) view.findViewById(R.id.goto_login);
        my_activities = (RelativeLayout) view.findViewById(R.id.my_activities);
        user_order = (RelativeLayout) view.findViewById(R.id.user_order);
        my_coupons = (RelativeLayout) view.findViewById(R.id.my_coupons);
        user_shopping_cart = (RelativeLayout) view.findViewById(R.id.user_shopping_cart);
        my_integral_center = (RelativeLayout) view.findViewById(R.id.my_integral_center);
        MobclickAgent.onEvent(getActivity(), "Personal", "个人tab");
    }


    @Override
    public void onResume() {
        super.onResume();
        loginState = SharePreferencesHelper.getInstance(getActivity()).getBoolean("loginState");
        if (loginState != false) {
            goto_login.setVisibility(View.GONE);
            String userName = MyApplication.getApp().getUser().getNickName();
            if (userName != null) {
                user_name.setText(userName);
            } else {
                user_name.setText("");
                ImageUtils.loadImageView(getActivity(), R.drawable.symbols_bitmap_head_portrait, head_img);
            }

            //头像地址
            String headImg = MyApplication.getApp().getUser().getHeadImg();
            if (headImg != null && !"".equals(headImg)) {
                ImageUtils.loadImageView(getActivity(), headImg, head_img);
            } else {
                ImageUtils.loadImageView(getActivity(), R.drawable.symbols_bitmap_head_portrait, head_img);
            }
        } else {
            user_name.setText("");
            goto_login.setVisibility(View.VISIBLE);
            ImageUtils.loadImageView(getActivity(), R.drawable.symbols_bitmap_head_portrait, head_img);
        }

    }

    @Override
    protected void initData() {
        userId = SharePreferencesHelper.getInstance(getActivity()).getString("userId");
        token = SharePreferencesHelper.getInstance(getActivity()).getString("token");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void initListener() {
        registerClickListener(user_proposal, user_msg, user_cooperation,
                user_share, settings, user_collection, user_follows,
                head_img, goto_login, my_activities, user_order, my_coupons,
                user_shopping_cart, my_integral_center
        );
    }

    @Override
    protected void clickEvent(View v) {
        switch (v.getId()) {
            case R.id.user_proposal:
                //建议反馈
                Intent intent1 = new Intent();
                intent1.setClass(getActivity(), FeedBackActivity.class);
                startActivity(intent1);
                MobclickAgent.onEvent(getActivity(), "MyFeedBack", "意见反馈");
                break;
            case R.id.user_msg:
                //消息中心
                if (loginState == false) {
                    Intent loginIntent = new Intent();
                    loginIntent.setClass(getActivity(), LoginActivity.class);
                    startActivity(loginIntent);
                } else {
                    Intent intent2 = new Intent();
                    intent2.setClass(getActivity(), MessageActivity.class);
                    startActivity(intent2);
                    MobclickAgent.onEvent(getActivity(), "MyMessages", "我的消息");
                }

                break;
            case R.id.user_cooperation:
                /*合伙入驻*/

                Intent intent3 = new Intent();
                intent3.setClass(getActivity(), SettledCooperationActivtiy.class);
                startActivity(intent3);
                MobclickAgent.onEvent(getActivity(), "MySettledCooperation", "合伙入驻");

                break;
            case R.id.user_share:
                //用户分享
                if (loginState == false) {
                    Intent loginIntent = new Intent();
                    loginIntent.setClass(getActivity(), LoginActivity.class);
                    startActivity(loginIntent);
                } else {
                    ShowPickDialog();
                }

                break;
            case R.id.settings:
                //用户设置
                if (loginState == false) {
                    Intent loginIntent = new Intent();
                    loginIntent.setClass(getActivity(), LoginActivity.class);
                    startActivity(loginIntent);
                } else {
                    Intent intent6 = new Intent();
                    intent6.setClass(getActivity(), UserSettingsActivity.class);
                    startActivity(intent6);
                }

                break;
            case R.id.user_collection:
                //用户收藏
                if (loginState == false) {
                    Intent loginIntent = new Intent();
                    loginIntent.setClass(getActivity(), LoginActivity.class);
                    startActivity(loginIntent);
                } else {
                    Intent intent4 = new Intent();
                    intent4.setClass(getActivity(), MyCollectionActivity.class);
                    startActivity(intent4);
                    MobclickAgent.onEvent(getActivity(), "MyCollection", "我的收藏");
                }
                break;
            case R.id.user_follows:
                //用户关注
                if (loginState == false) {
                    Intent loginIntent = new Intent();
                    loginIntent.setClass(getActivity(), LoginActivity.class);
                    startActivity(loginIntent);
                } else {
                    Intent intent5 = new Intent();
                    intent5.setClass(getActivity(), MyFollowsActivity.class);
                    startActivity(intent5);
                    MobclickAgent.onEvent(getActivity(), "MyFollows", "我的关注");
                }

                break;
            case R.id.head_img:
                //头像上传
                if (loginState == false) {
                    Intent loginIntent = new Intent();
                    loginIntent.setClass(getActivity(), LoginActivity.class);
                    startActivity(loginIntent);
                } else {
                    // toast("上传头像");
//                    Intent intent = new Intent(getActivity(), RefundRequestActivity.class);
//                    startActivity(intent);
                }
                break;
            case R.id.goto_login:
                Intent loginIntent = new Intent();
                loginIntent.setClass(getActivity(), LoginActivity.class);
                startActivity(loginIntent);
                break;
            case R.id.my_activities:
                /*我的活动*/
                if (loginState == false) {
                    Intent intent = new Intent();
                    intent.setClass(getActivity(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(getActivity(), MyActivitysActivity.class);
                    startActivity(intent);
                    MobclickAgent.onEvent(getActivity(), "MyActivitys", "我的活动");
                }
                break;
            case R.id.user_order:
                /*我的订单*/
                if (loginState == false) {
                    Intent loginIntents = new Intent();
                    loginIntents.setClass(getActivity(), LoginActivity.class);
                    startActivity(loginIntents);
                } else {
                    Intent myOrderIntent = new Intent();
                    myOrderIntent.setClass(getActivity(), MyOrderActivity.class);
                    startActivity(myOrderIntent);
                    MobclickAgent.onEvent(getActivity(), "MyOrder", "我的订单");
                }
                break;
            case R.id.my_coupons:
                /*我的优惠券*/
                if (loginState == false) {
                    Intent loginIntents = new Intent();
                    loginIntents.setClass(getActivity(), LoginActivity.class);
                    startActivity(loginIntents);
                } else {
                    Intent myOrderIntent = new Intent();
                    myOrderIntent.setClass(getActivity(), MyCouponsActivity.class);
                    startActivity(myOrderIntent);
                    MobclickAgent.onEvent(getActivity(), "MyCoupons", "我的优惠券");
                }
                break;
            case R.id.user_shopping_cart:
                /*我的购物车*/
                if (loginState == false) {
                    Intent loginIntents = new Intent();
                    loginIntents.setClass(getActivity(), LoginActivity.class);
                    startActivity(loginIntents);
                } else {
                    Intent shoppingCart = new Intent(getActivity(), MyShoppingCartsActivity.class);
                    startActivity(shoppingCart);
                    MobclickAgent.onEvent(getActivity(), "MyShoppingCart", "我的购物车");

                }
                break;
            case R.id.my_integral_center:
                /*积分中心*/
                if (loginState == false) {
                    Intent loginIntents = new Intent();
                    loginIntents.setClass(getActivity(), LoginActivity.class);
                    startActivity(loginIntents);
                } else {
                    Intent integralCenter = new Intent(getActivity(), IntegralCenterActivity.class);
                    startActivity(integralCenter);
                    MobclickAgent.onEvent(getActivity(), "MyIntegralCenter", "积分中心");
                }
                break;
            default:
                break;
        }
    }

    //弹出分享框
    private void ShowPickDialog() {
        if (selectImage == null) {
            selectImage = new ShareMenuPopupWindow(getActivity());
            selectImage.setOnShareClickListener(shareClickListener);
        }
        selectImage.showAtLocation(getActivity().findViewById(R.id.linearlayout_base), Gravity.BOTTOM, 0, 0);
    }

    private ShareMenuPopupWindow.OnShareClickListener shareClickListener = new ShareMenuPopupWindow.OnShareClickListener() {

        @Override
        public void onShareClick(int position) {
            screenshot(position);
        }
    };

    private void share() {
        //  String downUrl = SharePreferencesHelper.getInstance(getActivity()).getString("downUrl");
        ShareAction shareAction = new ShareAction(getActivity());
        UMImage thumb = new UMImage(getActivity(), R.drawable.app_icon);
        UMWeb web = new UMWeb("http://yididesign.cn/share.html");
        web.setTitle("艺滴-创意家的经纪人");//标题
        web.setThumb(thumb);  //缩略图
        web.setDescription("销售全球的好设计和好创意，分享艺术设计创意的人气社区");//描述
        if (sharePosition == 1) {
            shareAction.setCallback(umShareListener).withMedia(web).share();
        } else {
            shareAction.setCallback(umShareListener).withMedia(web).share();

        }
        switch (sharePosition) {

            case 0:
                shareAction.setPlatform(SHARE_MEDIA.WEIXIN); // 微信好友
                break;
            case 1:
                shareAction.setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE);// 微信朋友圈
                break;

        }
        shareAction.share();
    }

    public void screenshot(int position) {
//        if (!LoadDialog.isShowProgressDialog()) {
//            LoadDialog.showProgressDialog(getActivity(), "制作分享数据...", false, false);
        sharePosition = position;
        share();

        //   }
    }

    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA platform) {
            //分享开始的回调
            selectImage.dismiss();
        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            Log.d("plat", "platform" + platform);
            IntegralChangeInterface.IntegralChange(getActivity(), userId, token, "5", "1", "分享成功啦");
            MobclickAgent.onEvent(getActivity(), "MyShareApp", "分享APP");

            selectImage.dismiss();
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(getActivity(), " 分享失败啦", Toast.LENGTH_SHORT).show();
            if (t != null) {
                Log.d("throw", "throw:" + t.getMessage());
            }
            selectImage.dismiss();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(getActivity(), " 分享取消了", Toast.LENGTH_SHORT).show();
            selectImage.dismiss();
        }

    };

    public void onEventMainThread(EventUserInfo event) {
             /*头像*/
        String newHeadImg = event.getUserInfoBean().getHeadImg();
        if (newHeadImg != null) {
            //头像地址
            if (!newHeadImg.equals("")) {
                ImageUtils.loadImageView(getActivity(), newHeadImg, head_img);
            } else {
                ImageUtils.loadImageView(getActivity(), R.drawable.me_img_default, head_img);
            }


        }


            /*昵称*/
        String newNickName = event.getUserInfoBean().getNickName();
        if (newNickName != null) {
            user_name.setText(newNickName);
        }

    }
}
